﻿// --------------------------------------------------------------------------------------
// <copyright file="UserSession.cs" company="NGroup">
// Copyright (c) NGroup. All rights reserved.
// </copyright>
// --------------------------------------------------------------------------------------
// <history>
//     <change who="Bui Thanh Nam" date="2013.12.22">Create</change>
// </history>
// --------------------------------------------------------------------------------------

namespace NGroup.VTL.WebCore
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Web.SessionState;
    using System.Web;
    using System.Web.Security;

    public class UserSession
    {
        private static readonly string _sessionRoleIdKey = "_userroleidsessionkey_";
        private static readonly string _sessionUsername = "_usernamesessionkey_";
        private static readonly string _sessionPassword = "_passwordsessionkey_";
        private static readonly string _sessiondatetime = "_datetimesessionkey_";
        private static readonly string _sessionUserIdKey = "_useridsessionkey_";

        private HttpSessionState _session
        {
            get
            {
                return HttpContext.Current != null ? HttpContext.Current.Session : null;
            }
        }

        public int RoleID
        {
            get
            {
                if (_session != null)
                {
                    int roleId = -1;

                    if (int.TryParse(_session[_sessionRoleIdKey].ToString(), out roleId))
                    {
                        return roleId;
                    }
                }

                return -1;
            }

            set
            {
                if (_session != null)
                {
                    _session[_sessionRoleIdKey] = value;
                }
            }
        }

        public string Username
        {
            get
            {
                string username = null;

                if (_session != null)
                {
                    username = _session[_sessionUsername] != null ? _session[_sessionUsername].ToString() : string.Empty;
                }

                return username ?? string.Empty;
            }

            set
            {
                if (_session != null)
                {
                    _session[_sessionUsername] = value;
                }
            }
        }

        public string Password
        {
            get
            {
                string password = null;

                if (_session != null)
                {
                    password = _session[_sessionPassword].ToString();
                }

                return password ?? string.Empty;
            }

            set
            {
                if (_session != null)
                {
                    _session[_sessionPassword] = value;
                }
            }
        }

        public bool IsSessionExpired
        {
            get
            {
                DateTime sessionTime = SessionDateTime;
                return sessionTime == DateTime.MaxValue
                    || sessionTime.AddMinutes(FormsAuthentication.Timeout.TotalMinutes) <= DateTime.Now;
            }
        }

        public bool IsAuthenticated
        {
            get
            {
                return !string.IsNullOrEmpty(Username) && !IsSessionExpired;
            }
        }

        public DateTime SessionDateTime
        {
            get
            {
                object sessionTime;
                if (HttpContext.Current == null ||
                    (sessionTime = HttpContext.Current.Session[_sessiondatetime]) == null)
                {
                    return DateTime.MinValue;
                }

                return (DateTime)sessionTime;
            }

            set
            {
                HttpContext.Current.Session[_sessiondatetime] = value;
            }
        }

        public int UserID
        {
            get
            {
                if (_session != null)
                {
                    int userId = -1;

                    if (int.TryParse(_session[_sessionUserIdKey].ToString(), out userId))
                    {
                        return userId;
                    }
                }

                return -1;
            }

            set
            {
                if (_session != null)
                {
                    _session[_sessionUserIdKey] = value;
                }
            }
        }
    }
}
